package com.dianrui.moonfire.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dianrui.moonfire.R;
import com.dianrui.moonfire.bean.Regular;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RegularAdapter extends BaseAdapter {
    private List<Regular> list;
    private Context context;
    private int temp = -1;
    private int selectPosition =0;//用于记录用户选择的变量
    private RegularClickListener regularcLickListener;

    public RegularAdapter(Context context, List<Regular> list) {
        this.list = list;
        this.context = context;

    }

    public void refresh(List<Regular> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }

    public void setSelection(int position) {
        selectPosition = position;

    }


    public interface RegularClickListener {
        void onRegularClick(int postion, String name);

    }

    public void setOnButtonClickListener(RegularClickListener RegularcLickListener) {
        this.regularcLickListener = RegularcLickListener;
    }

    @Override
    public int getCount() {
        if (list== null) {
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }



    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_regular, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        if (null != list &&list.size()>0) {
            final Regular regular = list.get(position);
            holder.title.setText(regular.title);
            holder.checkBox.setId(position);//对checkbox的id进行重新设置为当前的position
            final View finalConvertView = convertView;
            holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        if (regularcLickListener != null) {
                            regularcLickListener.onRegularClick(position,regular.name);
                        }
                        if (temp != -1) {
                            //找到上次点击的checkbox,并把它设置为false,对重新选择时可以将以前的关掉
                            CheckBox tempcheckbox = finalConvertView.findViewById(temp);
                            if (tempcheckbox != null) {
                                tempcheckbox.setChecked(false);
                            }
                        }
                        temp = buttonView.getId();//保存当前选中的checkbox的id值
                    }
                }
            });
            holder.mainLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (regularcLickListener != null) {
                        regularcLickListener.onRegularClick(position,regular.name);
                    }
                    if (regular.isChecked()){
                        holder.checkBox.setChecked(true);
                    }else{
                        holder.checkBox.setChecked(false);
                    }

                }
            });
            if (position == temp){
                holder.checkBox.setChecked(true);
            }else {
                holder.checkBox.setChecked(false);
            }

        }
        return convertView;
    }


     class ViewHolder {
        @BindView(R.id.title)
        TextView title;
         @BindView(R.id.cb_month)
        CheckBox checkBox;
         @BindView(R.id.regular_layout)
         LinearLayout mainLayout;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
